//
//  NSObject+STModel.m
//  STNewTools
//
//  Created by stoneobs on 17/2/14.
//  Copyright © 2017年 stoneobs. All rights reserved.
//

#import "NSObject+STModel.h"
#import <objc/runtime.h>
@implementation NSObject (STModel)
-(void)setValue:(id)value forUndefinedKey:(NSString *)key;
{
    
    //赋值失败不会崩溃
}
//字典转模型
-(id)initWithProperties:(NSDictionary*)properties {
    if(self =[self init]){
        if (![properties isKindOfClass:[NSDictionary class]]) {
            return self;
        }
        NSMutableDictionary *dic = [properties mutableCopy];
        //遍历排除nil
        for(NSString *keys in [[dic allKeys]mutableCopy]){
            NSObject *obj = [dic objectForKey:keys];
            if([obj isKindOfClass:[NSNull class]]){
                //                [dic removeObjectForKey:keys];
                [dic setObject:@"" forKey:keys];
            }
        }
        
        NSObject *object = [properties objectForKey:@"id"];
        if(object){
            [dic removeObjectForKey:@"id"];
            [dic setObject:object forKey:@"ID"];
        }
        for(NSString *keys in [dic allKeys]){
            NSObject *obj = [dic objectForKey:keys];
            if(!obj){
                obj = [[NSObject alloc]init];
                [dic setValue:obj forKey:keys];
            }
        }
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
//模型转字典
- (NSDictionary*)dicWithModel:(id)model
{

    unsigned int outCount, i;
    
    objc_property_t *properties = class_copyPropertyList([model class], &outCount);
    
    NSMutableArray *array =[NSMutableArray array];
    for(i = 0; i < outCount; i++) {
        
        objc_property_t property = properties[i];
        
        fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
        [array addObject:[NSString stringWithFormat:@"%s",property_getName(property)]];
    }
    NSMutableDictionary *dic =  [[model dictionaryWithValuesForKeys:array] mutableCopy];
    //遍历排除nil
    for(NSString *keys in [[dic allKeys]mutableCopy]){
        NSObject *obj = [dic objectForKey:keys];
        if([obj isKindOfClass:[NSNull class]]){
            [dic removeObjectForKey:keys];
        }
    }
    return dic;
}
@end
